/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import nu.zoom.swing.desktop.common.BackendException;
import nu.zoom.swing.desktop.plugin.portunis.ApplicationPreferences;
import nu.zoom.swing.desktop.preferences.Preferences;

/**
 * @see nu.zoom.swing.desktop.plugin.portunis.ApplicationPreferences
 * @version $Revision: 1.3 $
 * @author $Author: johan $
 */
public class ApplicationPreferencesImpl implements ApplicationPreferences {

	private static final String REMEMBERED_KEYSTORES = "keystores";

	private static final String FILECHOOSER_START_DIRECTORY = "keystore.filechooser";

	private Preferences prefs;

	/**
	 * 
	 */
	public ApplicationPreferencesImpl(Preferences prefs) {
		super();
		this.prefs = prefs;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.gui.ApplicationPreferences#forgetKeyStore(java.io.File)
	 */
	public void forgetKeyStore(File file) throws BackendException {
		try {
			Set storeFileNames = prefs.getStrings(REMEMBERED_KEYSTORES);
			storeFileNames.remove(file.getAbsolutePath());
			prefs.setStrings(REMEMBERED_KEYSTORES, storeFileNames);
		} catch (Exception e) {
			throw new BackendException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.gui.ApplicationPreferences#getRemeberedKeyStores()
	 */
	public ArrayList<File> getRememberedKeyStores() throws BackendException {
		try {
			Iterator<String> filenameIterator = prefs.getStrings(
					REMEMBERED_KEYSTORES).iterator();
			ArrayList<File> storeFiles = new ArrayList<File>();
			while (filenameIterator.hasNext()) {
				storeFiles.add(new File(filenameIterator.next()));
			}
			return storeFiles;
		} catch (Exception e) {
			throw new BackendException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.gui.ApplicationPreferences#rememberKeyStore(java.io.File)
	 */
	public void rememberKeyStore(File file) throws BackendException {
		try {
			Set<String> storeFileNames = prefs.getStrings(REMEMBERED_KEYSTORES);
			storeFileNames.add(file.getAbsolutePath());
			prefs.setStrings(REMEMBERED_KEYSTORES, storeFileNames);
		} catch (Exception e) {
			throw new BackendException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.gui.ApplicationPreferences#getFileChooserStartDirectory()
	 */
	public String getFileChooserStartDirectory() throws BackendException {
		try {
			return prefs.getString(FILECHOOSER_START_DIRECTORY);
		} catch (Exception e) {
			throw new BackendException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.gui.ApplicationPreferences#setFileChooserStartDirectory(java.lang.String)
	 */
	public void setFileChooserStartDirectory(String string)
			throws BackendException {
		try {
			prefs.setString(FILECHOOSER_START_DIRECTORY, string);
		} catch (nu.zoom.swing.desktop.common.BackendException e) {
			throw new BackendException(e);
		}
	}
}
